package com.zt.rongda.employee.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.constant.Constants;
import com.zt.rongda.employee.inter.EmployeeMapper;
import com.zt.rongda.employee.model.Employee;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.Department;
import com.zt.system.model.SqlBuilder;
import com.zt.system.model.User;
import com.zt.system.service.BaseService;
import com.zt.system.service.IDepartmentService;
import com.zt.system.service.IEmployeeService;
import com.zt.system.service.IFieldAliasService;
import com.zt.system.service.IUserService;
import com.zt.util.StringUtil;

@Service
public class EmployeeService extends BaseService implements IEmployeeService ,IFieldAliasService{

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IUserService userService;

    /**
     * 查询
     * 
     * @param reqBean
     * @return
     */
    public JSONObject getTable(ReqBean reqBean) {
        String deptId = reqBean.getInfo("deptId");
        String codeOrName = StringUtils.trimToEmpty(reqBean.getInfo("codeOrName"));

        SqlBuilder sqlBuilder = new SqlBuilder("select * from EMPLOYEE a where 1 = 1 ");
        sqlBuilder.addDeptWhereIn("DEPT_ID", deptId);
        if (StringUtils.isNotBlank(codeOrName)) {
            sqlBuilder.appendSql(" and (EMP_CODE = ? or EMP_NAME like ?)", codeOrName, "%" + codeOrName + "%");
        }
        sqlBuilder.orderBy(" order by EMP_NAME");

        JSONObject jsonObject = queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
        return jsonObject;
    }

    @Override
    public List<Employee> getAll() {
        return employeeMapper.selectAllEmployees();
    }

    /**
     * 通过id查询
     * 
     * @param empId
     * @return
     */
    public Employee getById(String empId) {
        return employeeMapper.selectByPrimaryKey(empId);
    }

    @Override
    public List<Employee> getByDeptId(String deptId) {
        Map<String, Department> map = new HashMap<String, Department>();
        List<Department> list = departmentService.getDescendantAndSelf(deptId);
        for (Department department : list) {
            map.put(department.getId(), department);
        }

        List<Employee> employees = new ArrayList<Employee>();
        for (Employee employee : getAll()) {
            if (deptId.equals(employee.getDeptId())) {
                employees.add(employee);
                break;
            }

        }
        return employees;
    }

    @Override
    public List<Employee> getLeaderPosOfEmp(String empId, String posId) {
        return null;
    }

    @Override
    public List<Employee> getLeaderPosOfDept(String deptId, String posId) {
        return null;
    }

    /**
     * 新增
     * 
     * @param employee
     */
    public void add(Employee employee) {
        if (employee == null) {
            throw new BusinessException("员工不存在");
        }
        User user = new User();
        user.setCode(employee.getCode());
        user.setName(employee.getName());
        // user.setEmail(employee.getName());
        user.setGender(employee.getGender());
        user.setMobile1(employee.getMobile());
        user.setDeptId(employee.getDeptId());
        user.setType(Constants.UserType.EMPLOYEE);
        user.setState(Constants.UserState.ENABLE);
        user.setMakeTime(new Date());
        user.setOperator(employee.getOperator());
        userService.add(user);

        employee.setId(user.getId());// 手动设置Id

        employee.setMakeTime(new Date());
        employee.setModifyTime(employee.getMakeTime());
        employeeMapper.insert(employee);
    }

    /**
     * 修改
     * 
     * @param employee
     */
    public void update(Employee employee) {
        String empId = employee.getId();
        if (StringUtil.isEmpty(empId)) {
            throw new BusinessException("获取主键失败");
        }

        employee.setModifyTime(new Date());
        employeeMapper.updateByPrimaryKeySelective(employee);

        User user = new User();
        user.setId(employee.getId());
        user.setCode(employee.getCode());
        user.setName(employee.getName());
        // user.setEmail(employee.getName());
        user.setGender(employee.getGender());
        user.setMobile1(employee.getMobile());
        user.setDeptId(employee.getDeptId());
        // user.setType(Constants.UserType.EMPLOYEE);
        // user.setState(Constants.UserState.ENABLE);
        user.setMakeTime(new Date());
        user.setOperator(employee.getOperator());
        userService.update(user);
    }

    /**
     * 删除
     * 
     * @param empId
     */
    public void delete(String empId) {
        if (StringUtil.isEmpty(empId)) {
            throw new BusinessException("获取主键失败");
        }
        employeeMapper.deleteByPrimaryKey(empId);
        userService.delete(empId);
    }

    public List<Employee> queryEmpOfDept(List<String> deptIds) {
        return this.employeeMapper.selectEmpOfDepts(deptIds);
    }
    
    @Override
    public Map<String, Object> dealFieldAlias(List<Object> fields, String param) {
        JSONArray array = query(new SqlBuilder("select * from EMPLOYEE where 1= 1").addWhereIn(
                "EMP_ID", fields));
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject json = array.getJSONObject(i);
            map.put(json.getString("empId"), json);
        }
        return map;
    }

}
